package cn.sh.library.mgts.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.sql.ConnectionPoolDataSource;

import cn.sh.library.mgts.sparql.Namespace;
import cn.sh.library.mgts.sparql.SparqlExecution;
import virtuoso.jdbc4.VirtuosoConnectionPoolDataSource;
import virtuoso.jena.driver.VirtGraph;

public class TestSparql {

//	final static String EP = "http://172.21.107.5:8891/sparql";

	final static String EP = "http://127.0.0.1:8891/sparql";
	final static String QUERY_URI = Namespace.getNsPrefixString() 
			+ " select ?uri from <http://gj.library.sh.cn/graph/instance> where {?uri a pmb:Instance . filter exists{?uri bf:creator ?creator}} limit 10";
	final static String QUERY_P_O = Namespace.getNsPrefixString()
			+ " select ?p ?o from <%2$s> where {<%1$s> ?p ?o}";
	final static String G_INSTANCE = "http://gj.library.sh.cn/graph/instance";
	final static String G_PERSON = "http://gj.library.sh.cn/graph/person";

	static VirtGraph graph ;

	static ConnectionPoolDataSource dataSource;
	
	static {

		VirtuosoConnectionPoolDataSource vtDataSource = new VirtuosoConnectionPoolDataSource();
		try {
			vtDataSource.setServerName("127.0.0.1");//名人
			vtDataSource.setPortNumber(1111);
			vtDataSource.setUser("dba");
			vtDataSource.setPassword("Shlibrary123");
			vtDataSource.setCharset("UTF-8");
//			vtDataSource.setServerName("10.1.30.87");//名人
//			vtDataSource.setPortNumber(1111);
//			vtDataSource.setUser("dba");
//			vtDataSource.setPassword("Shlibrary123");
//			vtDataSource.setCharset("UTF-8");
			vtDataSource.setInitialPoolSize(5);
		} catch (Exception e) {
			// TODO: handle exception
		}

		dataSource = vtDataSource;
   }
	public static void main(String[] args) {
//		endpoint();

		graph = new VirtGraph(G_INSTANCE, dataSource);
//		int i = 0;
//		while (i<1000) {
//			try {
//				graph();
//				i++;
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
		test();
	}
	
	private static void graph() {

		Date start = new Date();
		List<Map<String, String>> uris = RDFUtils.transformListMap(SparqlExecution.vQuery(graph, QUERY_URI, new String[] {"uri"})) ;
		Date d1 = new Date();
		double avg = 0;
		System.out.println("1:grap");
		System.out.println(d1.getTime() - start.getTime());
		System.out.println("2:");
		for(Map<String, String> map : uris) {
			Date d2 = new Date();
			List<Map<String, String>> instance = RDFUtils.transformListMap(SparqlExecution.vQuery(graph, String.format(QUERY_P_O, map.get("uri"), G_INSTANCE), new String[] {"p", "o"}));
			for(Map<String, String> pro : instance) {
				if(pro.get("p") == "http://bibframe.org/vocab/creator") {
					SparqlExecution.vQuery(graph, String.format(QUERY_P_O, pro.get("o"), G_PERSON), new String[] {"p", "o"});
				}
			}
			Date d3 = new Date();
			System.out.println(d3.getTime() - d2.getTime());
		}
		Date end = new Date();
//		graph.getConnection().close();
		System.out.println("3:");
		System.out.println("graph:" +(end.getTime() - start.getTime()));
	}
	
	private static void endpoint() {
		Date start = new Date();
		List<Map<String, String>> uris = RDFUtils.transformListMap(SparqlExecution.vQuery(EP, QUERY_URI, new String[] {"uri"})) ;
		Date d1 = new Date();
		double avg = 0;
		System.out.println("1:");
		System.out.println(d1.getTime() - start.getTime());
		System.out.println("2:");
		for(Map<String, String> map : uris) {
			Date d2 = new Date();
			List<Map<String, String>> instance = RDFUtils.transformListMap(SparqlExecution.vQuery(EP, String.format(QUERY_P_O, map.get("uri"), G_INSTANCE), new String[] {"p", "o"}));
			for(Map<String, String> pro : instance) {
				if(pro.get("p") == "http://bibframe.org/vocab/creator") {
					SparqlExecution.vQuery(EP, String.format(QUERY_P_O, pro.get("o"), G_PERSON), new String[] {"p", "o"});
				}
			}
			Date d3 = new Date();
			System.out.println(d3.getTime() - d2.getTime());
		}
		Date end = new Date();
		System.out.println("3:");
		System.out.println(end.getTime() - start.getTime());
	}
	
	static void test() {
		List<Map<String, String>> sqllist = SQLHelper.getResultList("select distinct selfuri, value from mgts.dbo.xx_instance_import_1 where idx = '41'");
		java.util.Set<String> uris = new HashSet<>();
		Map<String, Map<String, String>> map1 = new HashMap<String, Map<String,String>>();
		sqllist.forEach((map) -> {
			uris.add(map.get("selfuri"));
			map1.put(map.get("selfuri"), map);
		});
		
		java.util.Set<String> uris1 = new HashSet<>();
		String quey = Namespace.getNsPrefixString()
				+ "select distinct ?s where {?s a bf:Instance;bf:acquisitionTerms ?o}";
		List<Map<String, String>> sparqlList = RDFUtils.transformListMap(SparqlExecution.vQuery("http://localhost:8890/sparql", quey, new String[] {"s"}));
		sparqlList.forEach((map) -> {
			uris1.add(map.get("s"));
		});
		
		uris.forEach(s -> {
			if(!uris1.contains("http://data.library.sh.cn/mgts/resource/instance/" + s)) {
//				System.out.println(s);
				String insert = "with<http://mgts.library.sh.cn/graph/instance> "
						+ "insert {<http://data.library.sh.cn/mgts/resource/instance/" + s 
						+ "> <http://id.loc.gov/ontologies/bibframe/acquisitionTerms> '" + map1.get(s).get("value") + "'}";
				System.out.println(insert);
				SparqlExecution.update(graph, insert);
//				System.out.println(map1.get(s));
			}
		});
		
	}
}
